/***********************************************************************************/
/* Code license:  GNU General Public License v3                                    */
/* Main page:     https://code.google.com/p/library-cross-platform/                */
/* Issue tracker: https://code.google.com/p/library-cross-platform/issues/list     */
/***********************************************************************************/

#include "qpopup.h"
#include <QApplication>
#include <QDebug>

QPopup *QPopup::pPopup = 0;

QPopup::QPopup() : QLabel(QApplication::activeWindow())
{
  setWindowFlags(Qt::WindowTitleHint);
  setFocusPolicy(Qt::WheelFocus);
  installEventFilter(this);
  setAutoFillBackground(true);
  setBackground();
}

QPopup::~QPopup()
{
  removeEventFilter(this);
}

void QPopup::setBackground(QColor color)
{
  QString style = "background-color: rgba(" +
                      QString::number(color.red())+ "," +
                      QString::number(color.green()) + "," +
                      QString::number(color.blue()) + "," +
                      QString::number(color.alpha()) + ");";
  setStyleSheet(style);
}


void QPopup::showPopup()
{
  setFocus();
  show();
}

void QPopup::hidePopup()
{
  hide();
}

void QPopup::show()
{
  QLabel::show();
}

void QPopup::hide()
{
  QLabel::hide();
}

void QPopup::text(const QString &text)
{
  setText(text);
  adjustSize();
}

bool QPopup::eventFilter(QObject *obj, QEvent *event)
{
    if(event->type() == QEvent::Leave)
    {
        hide();
    }
/*
    qDebug() << "eventFilter(...)" << event->type() ;
  if (event->type() == QEvent::FocusIn) {
      qDebug() << "focus IN" ;
  } else if (event->type() == QEvent::FocusOut) {
      qDebug() << "focus OUT" ;
  }
*/
  return QLabel::eventFilter(obj, event);
}

/*
void QPopup::focusUpdated(QWidget * old, QWidget * now)
{
    qDebug() << "focusUpdated" ;
  if(now != pPopup)
  {
    hide();
  }
}
*/
